// var n;
// var s = cc._decorator;
// var f = s.ccclass;
// var h = s.property;
// var c =
//     (s.menu,
//     (function (e) {
//         function t() {
//             var t = (null !== e && e.apply(this, arguments)) || this;
//             t.anim = null;
//             t.mask = null;
//             t.maskAni = null;
//             return t;
//         }
//         var r;
//         __extends(t, e);
//         r = t;
//         t.show = function () {
//             return this._instance ? this._instance.show() : Promise.resolve();
//         };
//         t.hide = function () {
//             return this._instance ? this._instance.hide() : Promise.resolve();
//         };
//         t.prototype.onLoad = function () {
//             r._instance = this;
//             cc.game.addPersistRootNode(this.node);
//             this.node.zIndex = 9999;
//             this.anim.node.active = !1;
//             this.mask.active = !1;
//         };
//         t.prototype.onDestroy = function () {
//             r._instance = null;
//             cc.game.removePersistRootNode(this.node);
//         };
//         t.prototype.show = function () {
//             var e = this;
//             return new Promise(function (t) {
//                 e.mask.active = !0;
//                 e.maskAni.active = !0;
//                 e.maskAni.width = 0;
//                 e.maskAni.height = 0;
//                 e.maskAni.getComponent(cc.Mask).inverted = !1;
//                 e.anim.node.active = !1;
//                 var r = Math.sqrt(cc.winSize.width * cc.winSize.width + cc.winSize.height * cc.winSize.height);
//                 cc.tween(e.maskAni)
//                     .to(0.33, {
//                         width: r,
//                         height: r
//                     })
//                     .call(function () {
//                         e.anim.node.active = !0;
//                         var r = e.anim.setAnimation(0, "show", !1);
//                         e.anim.setTrackCompleteListener(r, function () {
//                             t();
//                         });
//                     })
//                     .start();
//             });
//         };
//         t.prototype.hide = function () {
//             var e = this;
//             return new Promise(function (t) {
//                 e.anim.node.active = !0;
//                 var r = e.anim.setAnimation(0, "end", !1);
//                 e.anim.setTrackCompleteListener(r, function () {
//                     e.maskAni.getComponent(cc.Mask).inverted = !0;
//                     e.maskAni.width = 0;
//                     e.maskAni.height = 0;
//                     var r = Math.sqrt(cc.winSize.width * cc.winSize.width + cc.winSize.height * cc.winSize.height);
//                     cc.tween(e.maskAni)
//                         .to(0.33, {
//                             width: r,
//                             height: r
//                         })
//                         .call(function () {
//                             e.anim.node.active = !1;
//                             e.mask.active = !1;
//                             e.maskAni.active = !1;
//                             t();
//                         })
//                         .start();
//                 });
//             });
//         };
//         t.prototype.start = function () {};
//         t._instance = null;
//         __decorate([h(sp.Skeleton)], t.prototype, "anim", void 0);
//         __decorate([h(cc.Node)], t.prototype, "mask", void 0);
//         __decorate([h(cc.Node)], t.prototype, "maskAni", void 0);
//         return (r = __decorate([f], t));
//     })(cc.Component));
// exports.default = c;

const { ccclass, property, menu } = cc._decorator;

/**
 * @class LoadingManager
 * @extends cc.Component
 * @description 管理加载动画的类
 */
@ccclass
@menu("Custom/LoadingManager")
class LoadingManager extends cc.Component {
    /** @type {sp.Skeleton} 动画组件 */
    @property(sp.Skeleton)
    anim = null;

    /** @type {cc.Node} 遮罩节点 */
    @property(cc.Node)
    mask = null;

    /** @type {cc.Node} 遮罩动画节点 */
    @property(cc.Node)
    maskAni = null;

    /** @type {LoadingManager} 单例实例 */
    static _instance = null;

    /**
     * 显示加载动画
     * @returns {Promise<void>}
     */
    static show() {
        return this._instance ? this._instance.show() : Promise.resolve();
    }

    /**
     * 隐藏加载动画
     * @returns {Promise<void>}
     */
    static hide() {
        return this._instance ? this._instance.hide() : Promise.resolve();
    }

    /**
     * 加载完成时的回调
     */
    onLoad() {
        LoadingManager._instance = this;
        cc.game.addPersistRootNode(this.node);
        this.node.zIndex = 9999;
        this.anim.node.active = false;
        this.mask.active = false;
        this.maskAni.active = false;
    }

    /**
     * 销毁时的回调
     */
    onDestroy() {
        LoadingManager._instance = null;
        cc.game.removePersistRootNode(this.node);
    }

    /**
     * 显示加载动画
     * @returns {Promise<void>}
     */
    show() {
        return new Promise(resolve => {
            this.mask.active = true;
            this.maskAni.active = true;
            this.maskAni.width = 0;
            this.maskAni.height = 0;
            this.maskAni.getComponent(cc.Mask).inverted = false;
            this.anim.node.active = false;
            const maxLength = Math.sqrt(cc.winSize.width * cc.winSize.width + cc.winSize.height * cc.winSize.height);
            cc.tween(this.maskAni)
                .to(0.33, { width: maxLength, height: maxLength })
                .call(() => {
                    this.anim.node.active = true;
                    const animationState = this.anim.setAnimation(0, "show", false);
                    this.anim.setTrackCompleteListener(animationState, () => {
                        resolve();
                    });
                })
                .start();
        });
    }

    /**
     * 隐藏加载动画
     * @returns {Promise<void>}
     */
    hide() {
        return new Promise(resolve => {
            this.anim.node.active = true;
            const animationState = this.anim.setAnimation(0, "end", false);
            this.anim.setTrackCompleteListener(animationState, () => {
                this.maskAni.getComponent(cc.Mask).inverted = true;
                this.maskAni.width = 0;
                this.maskAni.height = 0;
                const maxLength = Math.sqrt(cc.winSize.width * cc.winSize.width + cc.winSize.height * cc.winSize.height);
                cc.tween(this.maskAni)
                    .to(0.33, { width: maxLength, height: maxLength })
                    .call(() => {
                        this.anim.node.active = false;
                        this.mask.active = false;
                        this.maskAni.active = false;
                        resolve();
                    })
                    .start();
            });
        });
    }

    start() {}
}

exports.default = LoadingManager;
